package com.ccx.coachsite.ui.manage;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerMainBase;

public class ManageGroupView extends OwerBaseView {

	private List<ManageGroupInfo> groups;
	private int width;

	private Button btnNew;
	private ProgressDialog waitingDialog;
	private GridView grid;
	private View groupContainerView, emptyView;

	public ManageGroupView(Context context, OwerMainBase superView) {
		super(context, superView);
		width = superView.getWidth();
		btnNew = (Button) findViewById(R.id.client_group_btn_new);
		grid = (GridView) findViewById(R.id.client_group_grid);
		groupContainerView = findViewById(R.id.client_group_container);
		emptyView = findViewById(R.id.client_no_group_container);

		btnNew.setOnClickListener(btnNewClickListener);
		loadData();
	}

	@Override
	protected boolean onLoadData() {
		groups = ManageProvider.getInstance().getGroups(mContext);
		if (groups == null)
			return false;
		return true;
	}

	@Override
	protected void doRefreshDataSuccess() {
		super.doRefreshDataSuccess();
		GroupAdapter adapter = new GroupAdapter();
		grid.setAdapter(adapter);

		if (adapter.getCount() <= 0) {
			groupContainerView.setVisibility(View.GONE);
			emptyView.setVisibility(View.VISIBLE);
		} else {
			groupContainerView.setVisibility(View.VISIBLE);
			emptyView.setVisibility(View.GONE);
		}
	}

	private void deleteGroup(ManageGroupInfo group) {
		// waitingDialog = new ProgressDialog(mContext);
		// waitingDialog.setMessage("请稍候");
		// waitingDialog.show();
		// RemoveGroupTask task = new RemoveGroupTask();
		// task.execute(group.getId());
	}

	private void onDeleteGroupSuccess() {
		if (waitingDialog != null) {
			waitingDialog.dismiss();
			waitingDialog = null;
		}
		Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
		loadData();
	}

	private void onDeleteGroupFail() {
		if (waitingDialog != null) {
			waitingDialog.dismiss();
			waitingDialog = null;
		}
		Toast.makeText(mContext, "删除分组失败，请重试", Toast.LENGTH_SHORT).show();
	}

	OnClickListener btnNewClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			SwitchView(ManageKey.ll_manage_add_group, groups);
		}
	};

	OnClickListener btnDeleteGroupClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			final ManageGroupInfo group = (ManageGroupInfo) v.getTag();
			new AlertDialog.Builder(mContext)
					.setTitle(R.string.common_tip)
					.setMessage("确定删除分组吗？")
					.setPositiveButton(R.string.common_button_confirm,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									deleteGroup(group);
								}
							})
					.setNegativeButton(R.string.common_button_cancell, null)
					.show();
		}
	};

	OnClickListener gridItemClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			ManageGroupInfo group = (ManageGroupInfo) v.getTag();
			List<Object> args = new ArrayList<Object>();
			args.add(groups);
			args.add(group);
			SwitchView(ManageKey.ll_manage_group_info, args);
		}
	};

	class GroupAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return groups.size();
		}

		@Override
		public Object getItem(int position) {
			return groups.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View
					.inflate(mContext, R.layout.client_group_item, null);
			ManageGroupInfo group = groups.get(position);
			View innerLayout = view
					.findViewById(R.id.client_group_item_inner_layout);
			innerLayout.getLayoutParams().width = (width - 25) / 2;
			innerLayout.getLayoutParams().height = (width - 25) / 2;
			// if (isLastLine(position))
			// ((RelativeLayout.LayoutParams)
			// innerLayout.getLayoutParams()).bottomMargin = 80;
			innerLayout.setBackgroundColor(fetchNextColor(position));

			TextView txtCount = (TextView) view
					.findViewById(R.id.client_group_item_count);
			txtCount.setText(String.valueOf(group.getCustomCount()));

			Button txtName = (Button) view
					.findViewById(R.id.client_group_item_name);
			txtName.setText(group.getName());
			txtName.setTag(group);
			txtName.setOnClickListener(gridItemClickListener);

			Button btnDelete = (Button) view
					.findViewById(R.id.client_group_btn_delete);
			// if (group.isSystem()) {
			btnDelete.setVisibility(View.GONE);
			// } else {
			// btnDelete.setTag(group);
			// btnDelete.setOnClickListener(btnDeleteGroupClickListener);
			// }
			return view;
		}

		// private boolean isLastLine(int position) {
		// if (groups.size() % 2 == 0)
		// return position >= groups.size() - 2;
		// else
		// return position >= groups.size() - 1;
		// }

		private int fetchNextColor(int position) {
			int colorKey = position % 4;
			switch (colorKey) {
			case 0:
				return 0xff57ae47;
			case 1:
				return 0xffb63f96;
			case 2:
				return 0xff3771b7;
			case 3:
				return 0xffdc3027;
			}
			return 0xff57ae47;
		}
	}

	class RemoveGroupTask extends AsyncTask<Integer, Integer, Boolean> {

		@Override
		protected Boolean doInBackground(Integer... params) {
			return ManageProvider.getInstance()
					.removeGroup(mContext, params[0]);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result)
				onDeleteGroupSuccess();
			else
				onDeleteGroupFail();
		}

	}
}
